#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/8/1  17:52
# @Author: 余浪人
# @email: yulangren520@gmail.com
import jieba, datetime
import jieba.analyse
from apps.api import api_bp
from flask import jsonify, request
from apps.api.public import save_img, apiErrors
from apps.lib.token import apiVerify_token
from apps.lib.public import get_date
from apps.models import db
from apps.models.content_model import TL_Column_model, TL_Content_Model
from apps.lib.public import add_tag
@api_bp .route ('/column',methods =['GET'])
@apiVerify_token
def column ():
    OOOO00O000O0O0000 =TL_Column_model .query .all ()
    O0OO0000O00O00000 ={"status":0 ,"message":"获取分类成功","columnList":[{"column_name":O0O00OOOO000O0O0O .column_name ,"column_id":O0O00OOOO000O0O0O .id ,"column_uuid":O0O00OOOO000O0O0O .uuid ,"column_order":O0O00OOOO000O0O0O .column_order ,"column_keyword":O0O00OOOO000O0O0O .column_keyword ,'column_description':O0O00OOOO000O0O0O .column_description ,}for O0O00OOOO000O0O0O in OOOO00O000O0O0000 ]}
    return jsonify (O0OO0000O00O00000 )
@api_bp .route ('/document',methods =['POST'])
@apiVerify_token
def document ():
    O0O00O0OOOO00000O =request .json .get ('data')if request .is_json else request .form .get ('data')
    if O0O00O0OOOO00000O :
        try :
            if TL_Content_Model .query .filter (TL_Content_Model .title ==O0O00O0OOOO00000O ['title']).count ():
                return jsonify ({"status":400 ,"message":"文档标题已存在!"})
            if apiErrors (TL_Column_model .query .filter_by (id =O0O00O0OOOO00000O ['superior']).first ()):
                return jsonify ({"status":404 ,"message":"分类栏目不存在!"})
            O0O00O0OOOO00000O ['content']=save_img (O0O00O0OOOO00000O ['content'])
            O0O00O0OOOO00000O ['keyword']=','.join (jieba .cut_for_search (O0O00O0OOOO00000O ['title']))
            O0O00O0OOOO00000O ['update_time']=datetime .datetime .strptime (O0O00O0OOOO00000O ['update_time'],'%Y-%m-%d')if O0O00O0OOOO00000O .get ('update_time')else get_date ()
            try :
                O0OOO00OOO000O000 =TL_Content_Model ()
                O0OOO00OOO000O000 .set_attrs (O0O00O0OOOO00000O )
                db .session .add (O0OOO00OOO000O000 )
                db .session .commit ()
            except :
                db .session .rollback ()
                return jsonify ({"status":401 ,"message":"发布异常!"})
            OOO000OO0OOO00000 =[O00OOOOO0O000O00O for O00OOOOO0O000O00O ,_OO0000O0O00OOOOO0 in jieba .analyse .textrank (O0O00O0OOOO00000O ['title'],withWeight =True )]
            add_tag (OOO000OO0OOO00000 )
            return jsonify ({"status":0 ,"message":"发布成功!"})
        except :
            return jsonify ({"status":404 ,"message":"处理异常!"})
    return jsonify ({"status":403 ,"message":"请添加发布数据!"})
@api_bp .route ('/column',methods =['DELETE'])
@apiVerify_token
def delete_column ():
    O0O0OOO00000O00O0 =request .json .get ('data')if request .is_json else request .form .get ('data')
    OOO00OO0OO00OO000 =O0O0OOO00000O00O0 .get ('column_id')
    if O0O0OOO00000O00O0 and isinstance (OOO00OO0OO00OO000 ,int ):
        O00OO00O00OOOOO0O =TL_Column_model .query .filter_by (id =OOO00OO0OO00OO000 ).first ()
        if apiErrors (O00OO00O00OOOOO0O ):return apiErrors (O00OO00O00OOOOO0O )
        try :
            db .session .delete (O00OO00O00OOOOO0O )
            db .session .commit ()
        except :
            db .session .rollback ()
            return jsonify ({"status":400 ,"message":"删除异常"})
        return jsonify ({"status":0 ,"message":"删除成功"})
    return jsonify ({"status":401 ,"message":"请求异常"})
